﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Net.Class;
using System.Text.RegularExpressions;

namespace Net
{
    public partial class FormPassword : Form
    {
        public FormPassword()
        {
            InitializeComponent();
        }

        public static FormPassword childform = null;
        public static FormPassword FormPasswordSignleton()
        {
            if (childform == null)
            {
                childform = new FormPassword();
            }
            return childform;
        }


        private void FormPassword_Load(object sender, EventArgs e)
        {
            this.textBox_username.Text = NetCommon.userName;
             //this.textBox_oldPassword.Text = NetCommon.passWord;
            textBox_username.Enabled = false;
            //textBox_oldPassword.Enabled = false;
            checkBox1_isCurrentUser.Checked = true;
           // button1_changPasswordConfirm.Enabled = false;
        }

        private void button2_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_changPasswordConfirm_Click(object sender, EventArgs e)
        {
         
            string username = textBox_username.Text.Trim();
            string oldPassword = textBox_oldPassword.Text.Trim();
            string newPassword = textBox3_newPassword.Text.Trim();
            string newPasswordAgain = textBox4_newPasswordAgain.Text.Trim();
            richTextBox1.Text = newPassword + "\n" + newPasswordAgain;//debug
            if ("".Equals(username) || "".Equals(oldPassword) || "".Equals(newPassword) || "".Equals(newPasswordAgain))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "信息不完整 ", "您填写的信息不完整,请检查", ToolTipIcon.Info);
                return;
            }
            if (newPassword.Length < 6 )
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "信息不正确 ", "输入的新密码必须不小于6位，请重新输入", ToolTipIcon.Info);
                return;
            }
            if ( newPassword.Length > 15)
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "信息不正确 ", "输入的新密码必须不大于15位，请重新输入", ToolTipIcon.Info);
                return;
            }
            if (!newPassword.Equals(newPasswordAgain))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "信息不正确 ", "两次输入的新密码不一致,请检查", ToolTipIcon.Info);
                return;
            }
            string postUrl = "http://oa.swnu.edu.cn/tysfrz/tysfrz_Mainvlt";
            string postData = "rydlm=" + username + "&oldkl=" + oldPassword + "&newkl=" + newPassword + "&newkl_ok=" + newPasswordAgain+"&nextpage=http://oa.swnu.edu.cn/tysfrz/jsp/yhzc/send.jsp?marked=1&moudleno=zh_kl_yhmodify";
            string returnContent = NetCommon.striphtml(GetPage.getPage(postUrl,postData));
            richTextBox1.Text = returnContent;
            if (returnContent == null || returnContent.Equals(""))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "连接错误 ", "无响应，请检查网络……", ToolTipIcon.Info);
                return;
            }
            string popMessage = "";
            Regex r = new Regex("人员管理");
            Match m = r.Match(returnContent);
            if (m.Success)  //修改成功
            {
                popMessage = "帐户密码修改成功！";
                if (MainForm.mainform.checkBox_saveUser.Checked && this.checkBox1_isCurrentUser.Checked)
                {
                    MainForm.mainform.txt_passWord.Text = newPassword;
                    popMessage += "我们已帮您保存您的新密码";
                }
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "修改成功 ", popMessage, ToolTipIcon.Info);
                this.Close();
            }
            else  //修改失败了
            {
                popMessage = "错误信息:账户不存在或输入的用户名/口令不正确！请检查";
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "修改失败 ", popMessage, ToolTipIcon.Info);
            }

        }

        private void checkBox1_isCurrentUser_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1_isCurrentUser.Checked)
            {
                this.textBox_username.Text = NetCommon.userName;
                //this.textBox_oldPassword.Text = NetCommon.passWord;
                textBox_username.Enabled = false;
                //textBox_oldPassword.Enabled = false;
            }
            else
            {
                textBox_username.Enabled = true;
                //textBox_oldPassword.Enabled = true;
            }
        }

        private void formClosing(object sender, FormClosingEventArgs e)
        {
            try
　　        {
　　        this.Dispose(true);
　　        childform = null;
　　        }
　　        catch(Exception ex)
            { MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "出现异常 ", ex.ToString(), ToolTipIcon.Info); }
　　     }

       

      

       
    }
}
